<template>
  <div class="login-container">
    <el-card class="box-card">
      <!-- 标题 -->
      <div class="title"></div>
      <!-- :model=“数据项”  ：rules=“规则对象” -->
      <el-form ref="form" :model="user" :rules="rules">
        <el-form-item prop="name">
          <el-input
            v-model="user.name"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            type="password"
            v-model="user.pass"
            prefix-icon="el-icon-lock"
            show-password
            placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item prop="repass">
          <el-input
            type="password"
            v-model="user.repass"
            prefix-icon="el-icon-lock"
            show-password
            placeholder="请再次输入密码">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reg">注册</el-button>
        </el-form-item>
      </el-form>
      <el-link
       type="primary"
        :underline="false"
        @click="$router.push('login')">
        已有账号，去登录
        </el-link>
    </el-card>
  </div>
</template>

<script>
import { regAPI } from '@/api/user'
export default {
  name: 'MyReg',
  data() {
    const validatePass2 = (rule, value, callback) => {
      // value 表示输入框的值
      // callback() 表示通过    callback(new Error('请再次输入密码'))表示不通过
      if (value === this.user.pass) {
        callback()
      } else {
        callback(new Error('两次密码不一致'))
      }
    }
    return {
      user: {
        name: '',
        pass: '',
        repass: ''
      },
      // 验证规则对象
      rules: {
        // 验证同户名规则
        name: [
          // required：ture 表示必填  trigger：blur 表示失去焦点时触发
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码的规则
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        // 验证确密码规则
        repass: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  // 和data平级，写methods方法
  methods: {
    reg() {
    // form 是 <el-form> 的ref属性值，必须有这个值
      this.$refs.form.validate(async valid => {
      // console.log(valid) // 验证通过valid=true;  验证不通过valid=false
        if (!valid) return // 验证不通过，则return
        // console.log('验证通过，可以提供数据了')
        const data = {
          username: this.user.name,
          password: this.user.pass,
          repassword: this.user.repass
        }
        const { data: res } = await regAPI(data)
        if (res.code === 0) {
        // 注册成功
          this.$message.success(res.message)
          this.$refs.form.resetFields() // 重置表单
          this.$router.push('/login') // 跳转到登录页
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background: url(../assets/images/login_bg.jpg) no-repeat;
  background-size: cover;
  height: 100%;

  .el-card{
    width: 400px;
    // height: 300px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);

    .title {
      height: 40px;
      background: url(@/assets/images/login_title.png) top center no-repeat;
    }
    .el-button{
      width: 100%;
    }
  }
}
</style>
